package com.bah.tract.client.ui;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import java.util.Vector;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.swing.JDialog;

import com.bah.tract.client.workstore.tol.TaxLocationProvider;
import com.bah.tract.service.Property;
import com.bah.tract.service.Task;

@Singleton
public class TOLPropertiesDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final TOLPropertiesPanel panel;
	private final TaxLocationProvider mTaxLocationProvider;
	private Task mTask;

	@Inject
	public TOLPropertiesDialog(@Named("defaultImage") final Image image,
			final TaxLocationProvider taxLocationProvider) {
		setTitle("TRACT - Time Online Task Properties");
		mTaxLocationProvider = taxLocationProvider;
		panel = new TOLPropertiesPanel();
		setIconImage(image);
		add(panel);
		pack();
	}

	public final void setTask(final Task task) {
		mTask = task;
		String taxCode = null;
		for (final Property p : task.getIdentifyingProperty()) {
			if ("number".equals(p.getName())) {
				panel.jLabel2.setText(p.getStringValue());
			} else if ("taxCode".equals(p.getName())) {
				taxCode = p.getStringValue();
			} else if ("teleWork".equals(p.getName())) {
				panel.jCheckBox1.setSelected(p.isBooleanValue());
			}
		}
		panel.jLabel6.setText(task.getDescription());

		if (taxCode != null) {
			String selected = null;
			for (final Map.Entry<String, String> entry : mTaxLocationProvider
					.getTaxLocations().entrySet()) {
				if (entry.getValue().equals(taxCode)) {
					selected = entry.getKey();
				}
			}
			panel.jComboBox1.setSelectedItem(selected);

		}
	}

	/**
	 * 
	 * @author 524506
	 */
	private class TOLPropertiesPanel extends javax.swing.JPanel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		/** Creates new form NewJPanel5 */
		public TOLPropertiesPanel() {
			initComponents();
		}

		/**
		 * This method is called from within the constructor to initialize the
		 * form. WARNING: Do NOT modify this code. The content of this method is
		 * always regenerated by the Form Editor.
		 */
		// <editor-fold defaultstate="collapsed" desc="Generated Code">
		private void initComponents() {

			jLabel1 = new javax.swing.JLabel();
			jLabel2 = new javax.swing.JLabel();
			jLabel3 = new javax.swing.JLabel();
			jLabel4 = new javax.swing.JLabel();
			jLabel5 = new javax.swing.JLabel();
			jLabel6 = new javax.swing.JLabel();
			jComboBox1 = new javax.swing.JComboBox();
			jCheckBox1 = new javax.swing.JCheckBox();
			jButton1 = new javax.swing.JButton();
			jButton1.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(final ActionEvent e) {
					TOLPropertiesDialog.this.setVisible(false);
				}
			});
			jButton2 = new javax.swing.JButton();
			jButton2.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(final ActionEvent e) {
					final String location = mTaxLocationProvider
							.getTaxLocations()
							.get(jComboBox1.getSelectedItem());
					for (final Property p : mTask.getIdentifyingProperty()) {
						if ("taxCode".equals(p.getName())) {
							p.setStringValue(location);
						} else if ("teleWork".equals(p.getName())) {
							p.setBooleanValue(jCheckBox1.isSelected());
						}
					}
					TOLPropertiesDialog.this.setVisible(false);
				}
			});
			setName("Form"); // NOI18N

			jLabel1.setText("Charge Number"); // NOI18N
			jLabel1.setName("jLabel1"); // NOI18N

			jLabel2.setName("jLabel2"); // NOI18N

			jLabel3.setText("Description"); // NOI18N
			jLabel3.setName("jLabel3"); // NOI18N

			jLabel4.setText("Tax Location"); // NOI18N
			jLabel4.setName("jLabel4"); // NOI18N

			jLabel5.setText("Telework"); // NOI18N
			jLabel5.setName("jLabel5"); // NOI18N

			jLabel6.setText("DP Task 2"); // NOI18N
			jLabel6.setName("jLabel6"); // NOI18N
			final Vector<String> options = new Vector<String>(
					mTaxLocationProvider.getTaxLocations().keySet());
			jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(options));

			jComboBox1.setName("jComboBox1"); // NOI18N

			jCheckBox1.setName("jCheckBox1"); // NOI18N

			jButton1.setText("Cancel"); // NOI18N
			jButton1.setName("jButton1"); // NOI18N

			jButton2.setText("OK"); // NOI18N
			jButton2.setName("jButton2"); // NOI18N

			final javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
					this);
			setLayout(layout);
			layout.setHorizontalGroup(layout
					.createParallelGroup(
							javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(
							layout.createSequentialGroup()
									.addContainerGap()
									.addGroup(
											layout.createParallelGroup(
													javax.swing.GroupLayout.Alignment.LEADING)
													.addComponent(jLabel1)
													.addComponent(jLabel3)
													.addComponent(jLabel4)
													.addComponent(jLabel5))
									.addPreferredGap(
											javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
									.addGroup(
											layout.createParallelGroup(
													javax.swing.GroupLayout.Alignment.LEADING)
													.addComponent(jCheckBox1)
													.addComponent(jLabel6)
													.addComponent(jLabel2)
													.addComponent(
															jComboBox1,
															javax.swing.GroupLayout.PREFERRED_SIZE,
															283,
															javax.swing.GroupLayout.PREFERRED_SIZE))
									.addContainerGap(22, Short.MAX_VALUE))
					.addGroup(
							javax.swing.GroupLayout.Alignment.TRAILING,
							layout.createSequentialGroup()
									.addContainerGap(264, Short.MAX_VALUE)
									.addComponent(jButton2)
									.addPreferredGap(
											javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
									.addComponent(jButton1).addContainerGap()));
			layout.setVerticalGroup(layout
					.createParallelGroup(
							javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(
							layout.createSequentialGroup()
									.addContainerGap()
									.addGroup(
											layout.createParallelGroup(
													javax.swing.GroupLayout.Alignment.BASELINE)
													.addComponent(jLabel1)
													.addComponent(jLabel2))
									.addPreferredGap(
											javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
									.addGroup(
											layout.createParallelGroup(
													javax.swing.GroupLayout.Alignment.BASELINE)
													.addComponent(jLabel3)
													.addComponent(jLabel6))
									.addPreferredGap(
											javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
									.addGroup(
											layout.createParallelGroup(
													javax.swing.GroupLayout.Alignment.BASELINE)
													.addComponent(jLabel4)
													.addComponent(
															jComboBox1,
															javax.swing.GroupLayout.PREFERRED_SIZE,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															javax.swing.GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(
											javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
									.addGroup(
											layout.createParallelGroup(
													javax.swing.GroupLayout.Alignment.BASELINE)
													.addComponent(jLabel5)
													.addComponent(jCheckBox1))
									.addPreferredGap(
											javax.swing.LayoutStyle.ComponentPlacement.RELATED,
											6, Short.MAX_VALUE)
									.addGroup(
											layout.createParallelGroup(
													javax.swing.GroupLayout.Alignment.BASELINE)
													.addComponent(jButton1)
													.addComponent(jButton2))
									.addContainerGap()));
		}// </editor-fold>

		// Variables declaration - do not modify
		private javax.swing.JButton jButton1;
		private javax.swing.JButton jButton2;
		private javax.swing.JCheckBox jCheckBox1;
		private javax.swing.JComboBox jComboBox1;
		private javax.swing.JLabel jLabel1;
		private javax.swing.JLabel jLabel2;
		private javax.swing.JLabel jLabel3;
		private javax.swing.JLabel jLabel4;
		private javax.swing.JLabel jLabel5;
		private javax.swing.JLabel jLabel6;
		// End of variables declaration

	}

}
